package com.nercel.dsj.gksales.shiro.config;

import com.nercel.dsj.gksales.shiro.RestShiroFilterFactoryBean;
import com.nercel.dsj.gksales.shiro.realm.JwtRealm;
import com.nercel.dsj.gksales.shiro.service.ShiroService;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

//import org.crazycake.shiro.RedisCacheManager;
//import org.crazycake.shiro.RedisManager;
//import org.crazycake.shiro.RedisSessionDAO;

/**
 * @author sunlin
 * @date 2018-12-10
 */
@Configuration
public class ShiroJwtConfiguration {

	@Bean(name = "lifecycleBeanPostProcessor")
	public  LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	@Bean
	public ShiroFilterChainDefinition shiroFilterChainDefinition() {
		DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
		return chainDefinition;
	}

	@Bean
	public FilterRegistrationBean delegatingFilterProxy(){
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		DelegatingFilterProxy proxy = new DelegatingFilterProxy();
		proxy.setTargetFilterLifecycle(true);
		proxy.setTargetBeanName("shiroFilter");
		filterRegistrationBean.setFilter(proxy);
		return filterRegistrationBean;
	}

	@Bean("shiroFilter")
	public ShiroFilterFactoryBean shiroFilterFactoryBean(
			@Qualifier("securityManager") SessionsSecurityManager securityManager, ShiroService shiroService) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new RestShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		shiroFilterFactoryBean.setFilters(shiroService.getCustomFilters());
		shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroService.getFilterChainMap());
		return shiroFilterFactoryBean;
	}

	@Bean(name = "securityManager")
	public SessionsSecurityManager defaultWebSecurityManager(JwtRealm defaultRealm) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRealm(defaultRealm);
		return securityManager;
	}

	@Bean(name = "defaultRealm")
	public JwtRealm getRealm() {
		JwtRealm realm = new JwtRealm();
		//realm.setCredentialsMatcher(new JWTCredentialsMatcher());
		return realm;
	}

	@Bean(name = "credentialsMatcher")
	public HashedCredentialsMatcher hashedCredentialsMatcher() {
		HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
		hashedCredentialsMatcher.setHashAlgorithmName("md5");
		hashedCredentialsMatcher.setHashIterations(1);
		hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
		return hashedCredentialsMatcher;
	}

	@Bean
	public SimpleCookie simpleCookie() {
		SimpleCookie simpleCookie = new SimpleCookie("sid");
		simpleCookie.setName("WEBSID");
		simpleCookie.setPath("/");
		return simpleCookie;
	}

}
